<template>
  <div class="productlist">
    <h3>商品列表</h3>
    <el-table
      :data="goodaddlist"
      style="
        width: 99%;
        border-radius: 4px;
        box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
      "
    >
      <!-- category: "(冒菜)套餐自选区"
ctime: "2020-05-26T03:49:57.000Z"
goodsDesc: "荤素双人套餐（配米饭两份+饮品两份）\n主要原料：其他"
id: 113
imgUrl: "http://127.0.0.1:5000/upload/imgs/goods_img/1590464996752.webp"
name: "荤素双人套餐（配米饭两份+饮品两份）"
price: 35.88
rating: 100
ratings: "[{\"username\":\"3******c\",\"rateTime\":1469281964000,\"rateType\":0,\"text\":\"很喜欢的粥\",\"avatar\":\"http://static.galileo.xiaojukeji.com/static/tms/default_header.png\"},{\"username\":\"2******3\",\"rateTime\":1469271264000,\"rateType\":0,\"text\":\"\",\"avatar\":\"http://static.galileo.xiaojukeji.com/static/tms/default_header.png\"},{\"username\":\"3******b\",\"rateTime\":1469261964000,\"rateType\":1,\"text\":\"\",\"avatar\":\"http://static.galileo.xiaojukeji.com/static/tms/default_header.png\"}]"
sellCount: 68 -->
      <!-- 下拉渲染 -->
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="商品ID">
              <span>{{ props.row.id }}</span>
            </el-form-item>
            <el-form-item label="所属分类">
              <span>{{ props.row.category }}</span>
            </el-form-item>
            <el-form-item label="商品图片">
              <span>
                <img :src="props.row.imgUrl" />
                <!-- {{ props.row.imgUrl }} -->
              </span>
            </el-form-item>
            <el-form-item label="店铺评价">
              <span>{{ props.row.rating }}</span>
            </el-form-item>
            <el-form-item label="商品描述">
              <span>{{ props.row.goodsDesc }}</span>
            </el-form-item>
            <el-form-item label="商品名称">
              <span>{{ props.row.name }}</span>
            </el-form-item>
            <el-form-item label="商品价格">
              <span>{{ props.row.price }}</span>
            </el-form-item>
            <el-form-item label="创建时间">
              <span>{{ props.row.ctime }}</span>
            </el-form-item>
            <el-form-item label="产品销量">
              <span>{{ props.row.sellCount }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="商品名称" prop="name"></el-table-column>
      <el-table-column label="所属分类" prop="category"></el-table-column>
      <el-table-column label="商品价格" prop="price"></el-table-column>
      <el-table-column label="商品图片">
        <template slot-scope="data">
          <img :src="data.row.imgUrl" class="proimg"
        /></template>
      </el-table-column>
      <el-table-column label="商品描述" prop="goodsDesc"> </el-table-column>
      <el-table-column
        label="操作"
        width="180px
      "
      >
        <template slot-scope="change">
          <el-button type="primary" @click="changename(change.row)"
            >编辑</el-button
          >
          <el-button type="danger" @click="delproduct(change.row.id)"
            >删除</el-button
          ></template
        >
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="nowpage"
        :page-sizes="pagetip"
        :page-size="showpage"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- 编辑页面 -->
    <el-dialog
      title="商品修改"
      :visible.sync="dialogFormVisible"
      @close="close"
    >
      <div>
        <span>商品名称</span>
        <div>
          <el-input v-model="inputname" placeholder="请输入商品名称"></el-input>
        </div>
      </div>
      <div>
        <span>商品分类</span>
        <div>
          <el-select v-model="choosevalue" placeholder="请选择商品分类">
            <el-option
              v-for="item in chooseoptions"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </div>
      </div>
      <div>
        <span>商品价格</span>
        <div>
          <el-input
            v-model="inputprice"
            placeholder="请输入商品价格"
          ></el-input>
        </div>
      </div>
      <div>
        <span>商品图片</span
        ><span v-show="showimg" style="color: red"> (请上传新图片)</span>
        <div>
          <el-upload
            style="
              width: 100px;
              height: 100px;
              border: 1px dashed #d9d9d9;
              border-radius: 6px;
            "
            class="avatar-uploader"
            action="http://127.0.0.1:5000/goods/goods_img_upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </div>
      </div>
      <div>
        <span>商品描述</span>
        <div>
          <el-input v-model="inputdesc" placeholder="请输入商品描述"></el-input>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="clickover">取 消</el-button>
        <el-button type="primary" @click="inputsure">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 引入获取商品列表ajax
// editgood
import { goodlist, delgood, editgood, categories } from "@/apis/items";
import { YMDHMS } from "@/utils/utils";
export default {
  data() {
    return {
      // 显示图片提示框
      showimg: false,
      // 当前在第几页
      nowpage: 1,
      // 总数据条数
      pagetip: [5, 10, 15, 20],
      // 当前页面显示多少条
      showpage: 5,
      // 分页条数
      total: 0,
      // 商品名称
      inputname: "",
      // 商品分类
      choosevalue: "",
      // 商品价格
      inputprice: "",
      // 商品描述
      inputdesc: "",
      // // 下拉选择的时间
      // selecttime: "",
      // 本机回显图片,
      imageUrl: "",
      // 模态框
      dialogFormVisible: false,
      // 商品列表
      goodaddlist: [],
      chooseoptions: [],
    };
  },
  methods: {
    // 当前页面显示多少条
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      // 先赋值给动态数据
      this.showpage = val;
      // 再调用函数,这时函数的参数等于赋值后的动态数据,所以会转换
      this.getgoodlist();
    },
    // 当前移动到哪个界面
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      // 先赋值给动态数据
      this.nowpage = val;
      // 再调用函数,这时函数的参数等于赋值后的动态数据,所以会转换
      this.getgoodlist();
    },
    // 封装获取当前商品列表的ajax函数
    getgoodlist() {
      // 发送ajax获取当前页面的列表
      goodlist({
        currentPage: this.nowpage,
        pageSize: this.showpage,
      }).then((res) => {
        // console.log(res);
        // 循环获得的数组
        for (const extime of res.data.data) {
          // 将时间转换格式,再赋给本身的值
          extime.ctime = YMDHMS(extime.ctime);
        }
        this.goodaddlist = res.data.data;
        this.total = res.data.total;
        // console.log(this.goodaddlist);
      });
    },
    // 删除数据
    delproduct(i) {
      // console.log(i);
      // 发送ajax,删除数据
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delgood({
            id: i,
          }).then((res) => {
            // console.log(res);
            if (res.data.code == 0) {
              this.$message({
                message: "恭喜你，删除商品成功",
                type: "success",
              });
              // 调用函数重新渲染表格
              this.getgoodlist();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 编辑
    changename(i) {
      this.dialogFormVisible = true;
      // 将当前点击编辑的当行数据渲染到模态框里面
      this.inputname = i.name;
      this.choosevalue = i.category;
      this.inputprice = i.price;
      this.imageUrl = i.imgUrl;
      this.inputdesc = i.goodsDesc;
      // 将id挂载到this上
      this.inputid = i.id;
    },
    // 上传图片
    handleAvatarSuccess(res, file) {
      // console.log(res, file);
      // console.log(file);
      // 本地渲染图片
      this.imageUrl = URL.createObjectURL(file.raw);
      // 将图片地址挂载到this指针
      this.newimg = res.imgUrl;
    },
    // 点击确定编辑按钮
    inputsure() {
      this.showimg = true;
      // 发送ajax请求,传参
      editgood({
        name: this.inputname,
        category: this.choosevalue,
        price: this.inputprice,
        imgUrl: this.newimg,
        goodsDesc: this.inputdesc,
        id: this.inputid,
      }).then((res) => {
        // console.log(res);
        // 请求成功时候;
        if (res.data.code == 0) {
          // 隐藏模态框
          this.dialogFormVisible = false;
          // 调用函数渲染页面
          this.getgoodlist();
          // 修改成功标示框
          this.$message({
            showClose: true,
            message: "恭喜你，商品修改成功",
            type: "success",
          });
          this.imgUrl = "";
          this.newimg = "";
          this.showimg = false;
        }
      });
    },
    // 点击取消事件
    clickover() {
      this.dialogFormVisible = false;
      this.showimg = false;
    },
    // dialog监听关闭事件
    close() {
      // console.log(1111);
      this.showimg = false;
    },
  },
  created() {
    this.getgoodlist();
    // 页面数据加载完毕后发送ajax请求,获得分类列表
    categories().then((res) => {
      // console.log(res.data.categories);
      let arr = [];
      // this.chooseoptions = res.data.categories;
      for (const obj of res.data.categories) {
        // console.log(obj.cateName);
        arr.push(obj.cateName);
      }
      // console.log(arr);
      this.chooseoptions = arr;
    });
  },
};
</script>

<style lang="less" scoped>
.productlist {
  margin: 20px;
  background-color: #fff;
  box-sizing: border-box;
  padding: 0 20px 20px 20px;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  h3 {
    line-height: 60px;
    border-bottom: 1px solid #ccc;
    margin-bottom: 10px;
  }
  .proimg {
    width: 60px;
    height: 60px;
  }
  .block {
    margin-top: 20px;
  }
  .demo-table-expand {
    .el-form-item {
      width: 500px;
      img {
        width: 60px;
        height: 60px;
      }
    }
  }
  .el-dialog {
    div {
      margin: 2px 0;
    }
  }
  .avatar-uploader .el-upload {
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
}
</style>